import os
import sys

from common.content_type import ContentType
from core.result_base import ResultBase
from api.project import project
from common.logger import logger
from common.user_agent import useragent
import urllib3
urllib3.disable_warnings()

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(BASE_PATH)


def project_create(access_token,_t,name,parent_id,_xsrf):
    """
    创建文件夹
    :param _t: 13位的时间戳
    :param name: 文件夹名称
    :param parent_id : 父文件夹，为空代表是根目录，不为空则代表有父目录
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "name": name,
        "parent_id": parent_id,
        "_xsrf":_xsrf
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
   }

    cookies = {
        "access_token":access_token
    }


    res = project.create(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("创建仪表盘文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def project_modify(access_token,_t,name,proj_id,_xsrf):
    """
    编辑文件夹
    :param _t: 13位的时间戳
    :param name: 文件夹名称
    :param proj_id : 要修改的文件夹id
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "data":{"name": name},
        "proj_id": proj_id,
        "_xsrf":_xsrf
    }

    header = {
        "Content-Type": "application/json;charset=utf-8"
   }

    res = project.modify(access_token,json=payload, headers=header)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("修改文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def project_tree(access_token,_t,_xsrf):
    """
    查询文件夹
    :param _t: 13位的时间戳
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "categories": [0,2],
        "_xsrf":_xsrf
    }

    header = {
        "Content-Type": "application/json;charset=utf-8"
   }

    res = project.tree(access_token,json=payload, headers=header)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("查询文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result



def project_delete(access_token,proj_id):
    """
    删除仪表盘文件夹
    :param proj_id :仪表盘文件夹ID
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "proj_id": proj_id
    }

    headers = {
        "Content-Type": ContentType.APPLICATION_JSON.value,
        "User-Agent": useragent.get_user_agent_one()
    }

    cookies = {
        "access_token": access_token
    }


    res = project.delete(params=payload, headers=headers,cookies=cookies)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("删除仪表盘文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result


def project_move(access_token,_t,proj_id,sort,_xsrf):
    """
    移动文件夹
    :param _t: 13位的时间戳
    :param _xsrf :
    :return: 自定义的关键字返回结果 result
    """
    result = ResultBase()
    payload = {
        "_t": _t,
        "proj_id": proj_id,
        "sort":sort,
        "_xsrf":_xsrf
    }

    header = {
        "Content-Type": "application/json;charset=utf-8",
        "access_token":"273f26cc95427aa615ac07c0983f7a3f7454a34ff8cf834f"
        #"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.66 Safari/537.36"
        # "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"

   }

    res = project.move(access_token,data=payload, headers=header)
    result.success = False
    if res.json()["status"] == "0":
        result.success = True
    else:
        result.error = "接口返回码是 【 {} 】, 返回信息：{} ".format(res.json()["errstr"], res.json())
    result.payload = res.json()
    result.response = res
    logger.info("移动文件夹 ==>> 返回结果 ==>> {}".format(result.response.text))
    return result




